home *** CD-ROM | disk | FTP | other *** search
- /*
- LoadExtension.c
-
- Loads an extension into the system heap. Assumes that the extension
- code resource is already in the system heap and locked (has system resource
- bit set). Detaches code if extension loads successfully. Extension should
- throw an exception if unsuccessful.
-
- Part of PatchWorks, the Extension Development Framework.
-
- by Mouse Herrell & Patrick Beard.
-
- Permission is granted to use this source code for any purpose, as long
- as the copyright notice is maintained.
-
- © 1992 Berkeley Systems, Inc.
- */
-
- #include <ShutDown.h>
- #include <Memory.h>
- #include <Resources.h>
-
- #include "Exceptions.h"
- #include "LoadExtension.h"
- #include "Patch.h"
- #include "Globals.h"
-
- /* prototypes. */
- void main(void);
- OSErr Startup(void);
- static pascal void RemoveAtShutDown(void);
-
- /* globals. */
- short theSystemVersion;
- Boolean theCQDFlag;
- Handle theExtensionCode;
-
- void main()
- {
- THz oldZone;
-
- #ifdef THINK_C
- InitGlobals();
- OpenGlobals();
-
- oldZone = GetZone();
- SetZone(SystemZone());
- #endif
-
- #ifdef applec
- oldZone = GetZone();
- SetZone(SystemZone());
-
- InitGlobals();
- OpenGlobals();
- #endif
-
- // get the handle to our code resource.
- theExtensionCode = GetCodeHandle();
-
- // if everything loaded ok, stay in memory.
- if (Startup() != noErr)
- DestroyGlobals();
- else
- DetachResource(theExtensionCode);
-
- CloseGlobals();
-
- SetZone(oldZone);
- }
-
- OSErr Startup(void)
- {
- SysEnvRec environment;
- #ifdef THINK_C
- long oldA5 = InitQD();
- #endif
- OSErr result = noErr;
-
- SysEnvirons(curSysEnvVers, &environment);
- theCQDFlag = environment.hasColorQD;
- theSystemVersion = environment.systemVersion;
-
- try {
- Install();
- } catch {
- Remove();
- result = theException;
- }
-
- #ifdef THINK_C
- SetA5(oldA5);
- #endif
-
- return result;
- }
-
- void InstallShutDownTask()
- {
- ShutDwnInstall((ShutDwnProcPtr)RemoveAtShutDown, sdOnUnmount);
- }
-
- static pascal void RemoveAtShutDown()
- {
- OpenGlobals();
- Remove();
- CloseGlobals();
- DestroyGlobals();
- }
-